package com.jic.market.impl;

import com.alibaba.fastjson.JSON;
import com.jic.market.entity.PromotionMemberGrade;
import com.jic.market.mapper.PromotionMemberGradeMapper;
import com.jic.market.request.MemberGradeInfoRequest;
import com.jic.market.request.PromotionMemberGradeRequest;
import com.jic.market.service.PromotionMemberGradeService;
import com.jic.member.provider.service.MemberUserProviderService;
import com.jic.member.request.MememberUserForMarket;
import com.jic.member.request.MememberUserForMarketRequest;
import com.jic.member.response.MememberUserForMarketResponse;
import com.jic.member.response.MememberUserResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yinhaijun
 * @date: 2020/6/30
 */
@Service
public class PromotionMemberGradeServiceImpl implements PromotionMemberGradeService {

    @Autowired
    private PromotionMemberGradeMapper promotionMemberGradeMapper;

    @Resource
    private MemberUserProviderService memberUserProviderService;

    @Override
    public Long addMemberGrade(MemberGradeInfoRequest request) {
        // 参数校验
        //
        List<PromotionMemberGradeRequest> promotionMemberGradeList = request.getPromotionMemberGradeList();
        if(null != promotionMemberGradeList && !promotionMemberGradeList.isEmpty()){
            List<PromotionMemberGrade> promotionMemberGrades = promotionMemberGradeList.stream().map(p -> {
                PromotionMemberGrade promotionMemberGrade = new PromotionMemberGrade();
                promotionMemberGrade.setPlatformId(p.getPlatformId());
                promotionMemberGrade.setPromotionId(request.getPromotionId());
                promotionMemberGrade.setPromotionCode(request.getPromotionCode());
                promotionMemberGrade.setPromType(request.getPromType());
                promotionMemberGrade.setFrontPromType(request.getFrontPromType());
                promotionMemberGrade.setDeleteFlag(false);
                promotionMemberGrade.setCreateTime(new Date());
                promotionMemberGrade.setUpdateTime(new Date());
                List<String> levelCodeList = p.getLevelCodeList();
                if (null != levelCodeList && !levelCodeList.isEmpty()) {
                    promotionMemberGrade.setGradeContent(JSON.toJSONString(levelCodeList));
                }
                return promotionMemberGrade;
            }).collect(Collectors.toList());
            promotionMemberGradeMapper.insertBatch(promotionMemberGrades);
//            for(PromotionMemberGradeRequest p : promotionMemberGradeList){
//                PromotionMemberGrade promotionMemberGrade = new PromotionMemberGrade();
//                promotionMemberGrade.setPlatformId(p.getPlatformId());
//                promotionMemberGrade.setPromotionId(request.getPromotionId());
//                promotionMemberGrade.setPromotionCode(request.getPromotionCode());
//                promotionMemberGrade.setPromType(request.getPromType());
//                promotionMemberGrade.setFrontPromType(request.getFrontPromType());
//                promotionMemberGrade.setDeleteFlag(false);
//                promotionMemberGrade.setCreateTime(new Date());
//                promotionMemberGrade.setUpdateTime(new Date());
//                List<String> levelCodeList = p.getLevelCodeList();
//                if(null != levelCodeList && !levelCodeList.isEmpty()){
//                    promotionMemberGrade.setGradeContent(JSON.toJSONString(levelCodeList));
//                }
//                promotionMemberGradeMapper.insert(promotionMemberGrade);
//            }
        }
        return null;
    }

    @Override
    public Long updateMemberGrade(MemberGradeInfoRequest request) {
        // 参数校验
        // 把以前的记录删除掉
        promotionMemberGradeMapper.deleteByPromotionCode(request.getPromotionCode());
        // 再次插入
        addMemberGrade(request);
        return null;
    }

    @Override
    public List<PromotionMemberGradeRequest> queryMemberGrade(String promotionCode) {
        PromotionMemberGrade promotionMemberGradeQ = new PromotionMemberGrade();
        promotionMemberGradeQ.setPlatformId(Long.valueOf(promotionCode));
        promotionMemberGradeQ.setDeleteFlag(false);
        List<PromotionMemberGrade> promotionMemberGradeList = promotionMemberGradeMapper.query(promotionMemberGradeQ);
        if(null != promotionMemberGradeList && !promotionMemberGradeList.isEmpty()){
            List<PromotionMemberGradeRequest> requestList = new ArrayList<>();
            for(PromotionMemberGrade memberGrade : promotionMemberGradeList){
                PromotionMemberGradeRequest request = new PromotionMemberGradeRequest();
                request.setPlatformId(memberGrade.getPlatformId());
                if(StringUtils.isNotBlank(memberGrade.getGradeContent())){
                    List<String> xxx = (List<String>) JSON.parse(memberGrade.getGradeContent());
//                    JSONObject json = JSONObject.parseObject(memberGrade.getGradeContent());
//                    List<String> levelCodeList = JSON.toJavaObject(json, List.class);
                    request.setLevelCodeList(xxx);
                }
                requestList.add(request);
            }
            return requestList;
        }
        return null;
    }

    @Override
    public Long deleteMemberGrade(String promotionCode) {
        promotionMemberGradeMapper.deleteByPromotionCode(promotionCode);
        return null;
    }

    @Override
    public Long copyMemberGrade(String oldPromotionCode, String promotionId, String promotionCode){
        // 查询出以前的，复制一份
        PromotionMemberGrade promotionMemberGradeQ = new PromotionMemberGrade();
        promotionMemberGradeQ.setPromotionCode(oldPromotionCode);
        List<PromotionMemberGrade> promotionMemberGradeList = promotionMemberGradeMapper.query(promotionMemberGradeQ);
        if(null != promotionMemberGradeList && !promotionMemberGradeList.isEmpty()){
            for(PromotionMemberGrade old : promotionMemberGradeList){
                old.setId(null);
                old.setPromotionId(promotionId);
                old.setPromotionCode(promotionCode);
                old.setDeleteFlag(false);
                old.setCreateTime(new Date());
                old.setUpdateTime(new Date());
                //promotionMemberGradeMapper.insert(old);
            }
            promotionMemberGradeMapper.insertBatch(promotionMemberGradeList);
        }

        return null;
    }

    @Override
    public Boolean checkPromotion(String thirdUserId, String channelCode, String channelCodes, List<PromotionMemberGrade> promotionMemberGradeList) {
        String grade = "";
        //查询会员所在渠道对应等级
        MememberUserForMarketRequest request = new MememberUserForMarketRequest();
        List<MememberUserForMarket> param = new ArrayList<>();
        MememberUserForMarket mememberUserForMarket = new MememberUserForMarket();
        Long[] deptIds = new Long[]{Long.parseLong(channelCode)};
        mememberUserForMarket.setDeptIds(deptIds);
        param.add(mememberUserForMarket);
        request.setParam(param);
        List<String> memThirdUserIdArr = new ArrayList<>();
        memThirdUserIdArr.add(thirdUserId);
        request.setMemThirdUserIdArr(memThirdUserIdArr);
        List<MememberUserForMarketResponse> mememberUserForMarketResponseList = memberUserProviderService.listMemberUserInfoForMarket(request);
        if(null != mememberUserForMarketResponseList && !mememberUserForMarketResponseList.isEmpty()){
            List<MememberUserResponse> mememberUserResponseList = mememberUserForMarketResponseList.get(0).getMememberUserResponseList();
            if(null != mememberUserResponseList && !mememberUserResponseList.isEmpty()){
                grade = mememberUserResponseList.get(0).getMemberGrade();
                // 找到渠道对应的等级
                List<String> gradeContent = getPromotionMemberGradeByPlat(channelCode, promotionMemberGradeList);
                // 比对该渠道对应等级是否在grade中
                if(null != gradeContent && !gradeContent.isEmpty()){
                    return gradeContent.contains(grade);
                }
            }
        }
        // 会员无等级系统或者渠道未对应任何等级直降返回true
        return true;
    }

    private List<String> getPromotionMemberGradeByPlat(String channelCode, List<PromotionMemberGrade> promotionMemberGradeList){
        if(null != promotionMemberGradeList && !promotionMemberGradeList.isEmpty()){
            for(PromotionMemberGrade promotionMemberGrade : promotionMemberGradeList){
                if(promotionMemberGrade.getPlatformId().toString().equals(channelCode)){
                    if(StringUtils.isNotBlank(promotionMemberGrade.getGradeContent())){
                        return (List<String>) JSON.parse(promotionMemberGrade.getGradeContent());
                    }
                }
            }
        }
        return null;
    }

}
